﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using static PracticeEveryday._250521;

namespace PracticeEveryday
{
    public delegate void ProcessingCompleteDelegate(string result);

    public class _250521
    {
        //1.简单自定义委托
        public delegate void MyDelegate(DateTime dateTime, string message);

        //2.多播委托
        public delegate void MultiCalDelegate(int a, int b);

        //3.委托回调

        //4.无返回值的Action委托
        public static Action<int, string>? Action;

        //5.有返回值的Func委托
        public static Func<int,int,int,int>? Func;

        //6.Predicate委托，返回bool
        public static Predicate<int>? predicate;

        //7.实现简单的事件系统
        public class AlarmClock
        {
            //1.定义委托类型
            public delegate void AlarmEventHandler(object sender,EventArgs args);

            //2.定义基于委托的事件
            public event AlarmEventHandler? Alarm;

            public void Start(TimeSpan delay)
            {
                Task.Delay(delay).ContinueWith(_ => { OnAlarm(); });
            }
            protected virtual void OnAlarm() 
            {
                Alarm?.Invoke(this, EventArgs.Empty);
            }
            
        }
        //8.委托实现策略模式

        static void Main()
        {
            //1
            MyDelegate myDelegate = new MyDelegate(SimpleDelegate);
            myDelegate(DateTime.Now, "五月");

            //2
            MultiCalDelegate multiCalDelegate = new MultiCalDelegate(AddCal);
            multiCalDelegate += SubCal;
            multiCalDelegate(5, 10);

            //3.
            var processor = new ProcessData();
            //Console.WriteLine("请输入要处理的文本");
            //var input = Console.ReadLine();
            //processor.ProcessingData(input, reslut => { Console.WriteLine(reslut); });

            //4.
            Action = new Action<int, string>(ActionProcess);
            Action(3,"五月二十一");

            //5.
            Func = new Func<int, int, int, int>(FuncProcess);//最后一位返回结果
            var result =  Func(1,2,3);
            Console.WriteLine($"Func委托：{result}");

            //6.
            predicate = num=>num%2==0;
            Console.WriteLine($"4是偶数吗:{predicate(4)}");

            //7.
            var clock = new AlarmClock();
            clock.Alarm += (sender, e) =>
            {
                Console.WriteLine("闹钟响了");
            };
            Console.WriteLine("设置3秒后响铃");
            clock.Start(TimeSpan.FromSeconds(2));

            Console.ReadLine();
        }
        public static void SimpleDelegate(DateTime dateTime, string message)
        {
            Console.WriteLine($"{dateTime},{message}");
        }

        public static void AddCal(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        public static void SubCal(int a, int b) => Console.WriteLine(b - a);

        public static void ActionProcess(int a, string message)
        {
            for (int i = 0; i < a; i++)
            {
                Console.WriteLine(message);
            }
        }
        public static int FuncProcess(int a, int b, int c)
        {
            return a + b + c;
        }
    }
    class ProcessData
    {
        public static void ProcessingData(string data, ProcessingCompleteDelegate callback)
        {
            Console.WriteLine("开始处理数据...");
            Thread.Sleep(1000);

            var result = data.ToUpper();
            callback(result);//执行回调
        }
    }
}

